#include "scr_state_splash.h"
#include "scr_state_title.h"
#include "scr_game.h"
#include "scr_music.h"
#include "scr_font.h"
#include "scr_game_state_manager.h"

SCRStateSplash::SCRStateSplash()
	: m_Time(0)
	, m_IsPressed(false)
{
}

void SCRStateSplash::OnInitialize()
{
	m_Font = m_pGame->GetResourceManager()->GetFont("fonts/carbon.ttf", 34);
	m_Music = m_pGame->GetResourceManager()->GetMusic("music/intro.ogg");
	m_Music->Play();
}

void SCRStateSplash::OnUpdate(const int& Delta)
{
	m_Time += Delta;
}

void SCRStateSplash::OnRender(const int& Delta)
{
	m_pGame->Clear();

	const int TimePerLetter = 120;
	const int TimeBetweenText = 3000;
	std::string Text0("Hot Rod Productions Presents");
	std::string Text1("A Game By Roderick Rivero");
	const int TotalTime0 = Text0.length() * TimePerLetter;
	const int TotalTime1 = Text1.length() * TimePerLetter;

	if (m_Time < TotalTime0 + TimeBetweenText)
	{
		std::string SubString = Text0.substr(0, m_Time / TimePerLetter);
		m_Font->Draw(SCRGame::SCR_SCREEN_WIDTH, SCRGame::SCR_SCREEN_HEIGHT / 2, SubString, 
			SCRFontType::SOLID, SCRFontAlign::RIGHT | SCRFontAlign::CENTER_Y);
	}
	else if (m_Time < TotalTime0 + TimeBetweenText + TotalTime1 + TimeBetweenText)
	{
		const int Time = m_Time - (TotalTime0 + TimeBetweenText);
		std::string SubString = Text1.substr(0, Time / TimePerLetter);
		m_Font->Draw(SCRGame::SCR_SCREEN_WIDTH, SCRGame::SCR_SCREEN_HEIGHT / 2, SubString, 
			SCRFontType::SOLID, SCRFontAlign::RIGHT | SCRFontAlign::CENTER_Y);
	}
	else if (!m_IsPressed)
	{
		m_IsPressed = true;
		m_pGame->GetStateManager()->SwitchTo<SCRStateTitle>();
	}
}

void SCRStateSplash::OnTerminate()
{

}

void SCRStateSplash::OnKeyClick(const int& Key)
{
	if (!m_IsPressed)
	{
		m_IsPressed = true;
		m_pGame->GetStateManager()->SwitchTo<SCRStateTitle>();
	}
}

void SCRStateSplash::OnMouseClick(const int& X, const int& Y, const SCRMouseButton::Enum& Button)
{
	if (!m_IsPressed)
	{
		m_IsPressed = true;
		m_pGame->GetStateManager()->SwitchTo<SCRStateTitle>();
	}	
}